/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginDialog.java
 *
 */
package gui;

import acc.DataInteraction;
import acc.LoginInteraction;
import acc.PassEncrypt;
import acc.Validation;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import gui.ShowMessageDialog;

/**
 *
 * @author Haitm
 */
public class LoginDialog extends javax.swing.JDialog {

    public static int x;
    public static int y;

    /**
     * Creates new form LoginDialog
     */
    public LoginDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        DataInteraction.configServer();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlLogin = new javax.swing.JPanel();
        btnConfig = new javax.swing.JButton();
        txtUsername = new javax.swing.JTextField();
        lblPassword = new javax.swing.JLabel();
        lblUserName = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        lblConfig = new javax.swing.JLabel();
        btnExit = new javax.swing.JButton();
        txtPassword = new javax.swing.JPasswordField();
        lblBanner = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Campaign Information System"); // NOI18N
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlLogin.setBackground(new java.awt.Color(255, 255, 255));
        pnlLogin.setName("pnlLogin"); // NOI18N
        pnlLogin.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnConfig.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnConfig.setForeground(new java.awt.Color(231, 0, 0));
        btnConfig.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/btn_Config_mini.png"))); // NOI18N
        btnConfig.setText("Config");
        btnConfig.setContentAreaFilled(false);
        btnConfig.setFocusable(false);
        btnConfig.setMargin(new java.awt.Insets(0, 2, 0, 2));
        btnConfig.setName("btnConfig"); // NOI18N
        btnConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfigActionPerformed(evt);
            }
        });
        pnlLogin.add(btnConfig, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 212, 70, 20));

        txtUsername.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtUsername.setName("txtUsername"); // NOI18N
        pnlLogin.add(txtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 92, 160, -1));

        lblPassword.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblPassword.setText("Password:");
        lblPassword.setName("lblPassword"); // NOI18N
        pnlLogin.add(lblPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 132, -1, -1));

        lblUserName.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblUserName.setText("Username:");
        lblUserName.setName("lblUserName"); // NOI18N
        pnlLogin.add(lblUserName, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 92, -1, -1));

        btnLogin.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnLogin.setForeground(new java.awt.Color(3, 97, 145));
        btnLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/adminMinii..png"))); // NOI18N
        btnLogin.setText("Login");
        btnLogin.setMargin(new java.awt.Insets(0, 2, 0, 2));
        btnLogin.setName("btnLogin"); // NOI18N
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        pnlLogin.add(btnLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 172, 80, -1));

        lblConfig.setForeground(new java.awt.Color(153, 153, 153));
        lblConfig.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblConfig.setText("Click here to config server");
        lblConfig.setName("lblConfig"); // NOI18N
        pnlLogin.add(lblConfig, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 212, 170, -1));

        btnExit.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnExit.setForeground(new java.awt.Color(3, 97, 145));
        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/btn_logout_minii..png"))); // NOI18N
        btnExit.setText("Exit");
        btnExit.setMargin(new java.awt.Insets(0, 2, 0, 2));
        btnExit.setName("btnExit"); // NOI18N
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });
        pnlLogin.add(btnExit, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 172, 70, -1));

        txtPassword.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtPassword.setName("txtPassword"); // NOI18N
        txtPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPasswordActionPerformed(evt);
            }
        });
        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPasswordKeyPressed(evt);
            }
        });
        pnlLogin.add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 132, 160, 20));

        lblBanner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Banner_Login.png"))); // NOI18N
        lblBanner.setName("lblBanner"); // NOI18N
        pnlLogin.add(lblBanner, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 2, -1, -1));

        getContentPane().add(pnlLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 240));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfigActionPerformed

        if (!flag) {
            flag = !flag;
            btnConfig.setText("Hide");
            lblConfig.setText("Click here to hide Config Panel");
            this.setSize(330, 450);
            sc.setVisible(false);
            this.add(sc, new AbsoluteConstraints(0, 220, 330, 180));
            sc.setVisible(true);
            pack();
        } else {
            flag = !flag;
            btnConfig.setText("Config");
            lblConfig.setText("Click here to config server");
            this.remove(sc);
            this.setSize(340, 250);
            pack();
        }

    }//GEN-LAST:event_btnConfigActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        login();
    }//GEN-LAST:event_btnLoginActionPerformed

    private String convert(char[] a) {
        String b = "";
        for (int i = 0; i < a.length; i++) {
            b += a[i];
        }
        return b;
    }

    private void login() {
        PassEncrypt p = new PassEncrypt();
        Vector users = LoginInteraction.getUser();
        status = false;
        if (users.size() > 0) {
            txtUsername.setBackground(java.awt.Color.white);
            txtPassword.setBackground(java.awt.Color.white);
            if (!Validation.checkContent2(txtUsername.getText())) {
                Validation.showMessage("Type valid user name here");
                txtUsername.setBackground(new java.awt.Color(250, 255, 168));
                txtUsername.requestFocus();
                return;
            }
            if (!Validation.checkContent2(convert(txtPassword.getPassword()))) {
                Validation.showMessage("Type valid password here");
                txtPassword.setBackground(new java.awt.Color(250, 255, 168));
                txtPassword.requestFocus();
                return;
            }

            for (int i = 0; i < users.size(); i++) {
                Vector temp = (Vector) users.elementAt(i);
                if (txtUsername.getText().equals(temp.elementAt(1).toString()) && p.encryptPass(txtPassword.getText()).equals(temp.elementAt(2).toString())) {
                    if (temp.elementAt(13).toString().trim().equalsIgnoreCase("1")) {
//                        JOptionPane.showMessageDialog(this, "no nhay vao day roi");
                    } else if (temp.elementAt(13).toString().trim().equalsIgnoreCase("0")) {
                        status = false;
                        new ShowMessageDialog(null, true, "Your account have been baned, please contact to admin!!!", ShowMessageDialog.MESSAGE);
//                        login();
                        txtUsername.requestFocus();
                        return;

//                     JOptionPane.showMessageDialog(this, temp.elementAt(13).toString().trim());
                    }
//                    String elementAt = temp.elementAt(13).toString();
//                    System.out.println(elementAt);
                    status = true;
                    //test print all element in vector
                    //all info of user
                    printTemp(temp);
                    String permission = temp.elementAt(12).toString().trim();
                    if (permission.equalsIgnoreCase("administrator")) {
                        entity.Permission.ADMINISTRATOR = true;
                    } else if (permission.equalsIgnoreCase("campaign master")) {
                        entity.Permission.CAMPAIGN_MASTER = true;
                    } else if (permission.equalsIgnoreCase("product master")) {
                        entity.Permission.PRODUCT_MASTER = true;
                    } else if (permission.equalsIgnoreCase("estimated budget")) {
                        entity.Permission.ESTIMATED_BUDGET = true;
                    } else if (permission.equalsIgnoreCase("actual budget")) {
                        entity.Permission.ACTUAL_BUDGET = true;
                    } else if (permission.equalsIgnoreCase("statistics")) {
                        entity.Permission.STATISTICS = true;
                    } else if (permission.equalsIgnoreCase("read only")) {
                        entity.Permission.READ_ONLY = true;
                    }

                    entity.Permission.UserName = txtUsername.getText();
                    entity.Permission.Password = convert(txtPassword.getPassword());
                    userName = txtUsername.getText();
                    dispose();
                }

            }

            if (!status) {
                new ShowMessageDialog(null, true, "Login false, check your username and password!!", ShowMessageDialog.MESSAGE);
                txtUsername.requestFocus();
            }
        } //        else if(users.size()==0){
        //            new ShowMessageDialog(null, true, "No User in database, let's create one now. The first User have bean created will be the Administrator account by default!!",ShowMessageDialog.MESSAGE);
        //            CreateAccountDialog newAcc = new CreateAccountDialog(null, true);            
        //        }
        else {
            new ShowMessageDialog(null, true, "Check your Server Config", ShowMessageDialog.MESSAGE);
        }
    }

    private void txtPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            login();
        }
    }//GEN-LAST:event_txtPasswordKeyPressed

    private void txtPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPasswordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPasswordActionPerformed

    public static boolean getStatus() {
        return status;
    }

    public static void setStatus(boolean Status) {
        status = Status;
    }

    public static String getUsername() {
        return userName;
    }
    public static String userName;
    private static boolean flag = false;
    private static boolean status = false;
    ServerConfig sc = new ServerConfig();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConfig;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel lblBanner;
    private javax.swing.JLabel lblConfig;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUserName;
    private javax.swing.JPanel pnlLogin;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

    public static void printTemp(Vector temp) {
        for (int i = 0; i < temp.size(); i++) {
            System.out.println(i + "" + temp.elementAt(i));
        }
    }
}
